package com.meituan.mapper;

import com.meituan.model.ShopEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ShopMapper {
    @Select("select * from shop where merchant_id = #{merchantId}")
    @Results({
            @Result(property = "shopId",column = "shop_id"),
            @Result(property = "classifyList",column = "shop_id",
                    many = @Many(select = "com.meituan.mapper.GoodsClassifyMapper.findByShopIdWithGoods",fetchType = FetchType.EAGER)),
    })
    List<ShopEntity> findAllByMerchantIdWithClassify(String merchantId);

    @Insert("insert into shop(merchant_id,shop_name,shop_phone,shop_address,shop_notice,shipping_fee,register_time,delivery_time,office_time,closing_time)" +
            "values(#{merchantId},#{shopName},#{shopPhone},#{shopAddress},#{shopNotice},#{shippingFee},#{registerTime},#{deliveryTime},#{officeTime},#{closingTime})")
    Integer save(ShopEntity shopEntity);

    @Delete("delete from shop where shop_id =#{shopId}")
    Integer delete(String shopId);

    @Select("select * from shop")
    @Results({
            @Result(property = "shopId",column = "shop_id"),
            @Result(property = "monthSales",column = "shop_id",
            one = @One(select = "com.meituan.mapper.UtilMapper.findCountByShopId",fetchType = FetchType.EAGER)),
            @Result(property = "avgScore",column = "shop_id",
            one = @One(select = "com.meituan.mapper.UtilMapper.findAvgScoreByShopId",fetchType = FetchType.EAGER)),
            @Result(property = "discountList",column = "shop_id",
                    many = @Many(select="com.meituan.mapper.DiscountMapper.findAllByShopId",fetchType = FetchType.EAGER)),
            @Result(property = "goodsList",column = "shop_id",
                    many = @Many(select="com.meituan.mapper.GoodsMapper.findByShopId",fetchType = FetchType.EAGER))
    })
    List<ShopEntity> findAllWithDiscount();

    @Select("select * from shop where shop_name = #{shopName}")
    ShopEntity findByName(String shopName);

    @Update("update shop set shop_name=#{shopName},shop_phone=#{shopPhone},shop_address=#{shopAddress},shipping_fee=#{shippingFee},shop_notice=#{shopNotice},delivery_time=#{deliveryTime},office_time=#{officeTime},closing_time=#{closingTime} where shop_id=#{shopId}")
    Integer updateShop(ShopEntity shopEntity);

    @Update("update shop set shop_img=#{shopImg} where shop_id=#{shopId}")
    Integer updateShopImg(@Param("shopId") String shopId,@Param("shopImg") String shopImg);

    @Select("select * from shop where shop_id=#{shopId}")
    @Results({
            @Result(property = "shopId",column = "shop_id"),
            @Result(property = "discountList",column = "shop_id",
                    many = @Many(select="com.meituan.mapper.DiscountMapper.findAllByShopId",fetchType = FetchType.EAGER))
    })
    ShopEntity findById(String shopId);

}
